package com.woniuxy.main.handler;

import com.woniuxy.main.pojo.FinallyOrderDTO;
import com.woniuxy.main.pojo.Order;
import com.woniuxy.main.pojo.OrderInfo;
import com.woniuxy.main.pojo.Staff;
import com.woniuxy.main.service.OrderService;
import com.woniuxy.main.utils.GenerateOrderNumber;
import org.apache.shiro.authc.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2019-9012, sun sss info. Co., Ltd.
 *
 * @author Su
 * @version 1.0
 * @InterfaceName: OrderHandler
 * @Description: 订单视图层
 * @date 2019/8/1 13:18
 **/
@Controller
@RequestMapping("/order")
public class OrderHandler {
    @Resource
    private OrderService orderService;

    /**
     * 显示 所有订单详情及所属宾客和操作员工
     *
     * @return 房间Map集合
     * @throws Exception 全部
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map findAll() throws Exception {
        List<Order> orderList = orderService.findAll();
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", orderList.size());
        result.put("data", orderList);
        return result;
    }

    /**
     * 根据输入订单号 模糊查询订单
     *
     * @return 房间Map集合
     * @throws Exception 全部
     */
    @RequestMapping("/findByOrdernum")
    public @ResponseBody
    Map findByOrdernum(@RequestParam(name = "ordernum") String ordernum) throws Exception {

        ordernum = "%" + ordernum + "%";
        List<Order> orderList = orderService.findByOrdernum(ordernum);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", orderList.size());
        result.put("data", orderList);
        return result;
    }


    /**
     * 获取 后台生成的订单号
     *
     * @return 订单号字符串
     * @throws Exception 全部
     */
    @RequestMapping("/generateOrder")
    public @ResponseBody
    String generateOrder() throws Exception {
        String generateOrder = GenerateOrderNumber.generateOrder();
        return generateOrder;
    }


    /**
     * 修改 订单状态
     *
     * @param order_id 订单id
     * @param status   要修改成的状态
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    public String updateOrder(@RequestParam(name = "order_id") String order_id,
                              @RequestParam(name = "status") Integer status) throws Exception {

        boolean b = orderService.updateOrder(order_id, status);
        return " ";
    }

    /**
     * 新增 订单
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/add")
    public  @ResponseBody String addOrder(@RequestParam(name="startdate", required=true)String startdate,
                                          @RequestParam(name="enddate", required=true)String enddate,
                                          @RequestParam(name="roomIdList", required=true)String roomIdList)
            throws Exception {
        boolean flag=false;
        //非空判断
        if(startdate!=null&&enddate!=null&&roomIdList!=null){
            System.out.println(startdate);
            System.out.println(enddate);
            //得到要入住的房间id集合
            List<String> roomIds =Arrays.asList(roomIdList.split(","));
            System.out.println(roomIds);
             flag = orderService.addOrder(startdate, enddate, roomIds);
        }


        return " "+flag;
    }
    /*
    生成订单
     */
    @RequestMapping("/create")
    public String createOrder(String order_id, Model model) throws Exception{
        System.out.println("进入生成订单页面");

        FinallyOrderDTO finallyOrderDTO=orderService.createOrder(order_id);

        System.out.println("*********"+finallyOrderDTO.getOrder().getOrdernum());
        model.addAttribute("finallyOrderDTO",finallyOrderDTO);

        return "pagefile/indent/orderitem";

    }
    /*
    确认订单
     */
    @RequestMapping("/sureOrder")
    @ResponseBody
    public String sureOrder(String order_id)  throws Exception{
        System.out.println("确认订单");

        boolean b=orderService.chagneStuts(order_id);

        if(b){
            return "0";
        }
        return "1";

    }
}
